package org.willa.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.willa.business.login.entity.LoginUser;
import org.willa.business.sys.entity.SysUser;
import org.willa.common.enums.UserLoginExceptionTypeEnum;
import org.willa.common.exception.UserLoginException;
import org.willa.system.mapper.SysUserMapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author wuwei
 * @date 2024/1/2
 * @apiNote
 */
@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername,username));
        if (Objects.isNull(sysUser)) {
            throw new UserLoginException(UserLoginExceptionTypeEnum.NO_USER);
        }


        // TODO 查询对应权限信息(待实现)
        LoginUser obj = new LoginUser(sysUser, new ArrayList<String>());

        return obj;
    }
}
